package com.itheima.app.manager;

import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.exception.NumberException;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Voice;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserVoiceService;
import com.itheima.service.mongo.VoiceService;
import com.itheima.vo.VoiceVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
public class VoiceManager {

    @Autowired
    private OssTemplate ossTemplate;

    @DubboReference
    private VoiceService voiceService;

    @DubboReference
    private UserVoiceService userVoiceService;

    @DubboReference
    private UserInfoService userInfoService;

    public void saveVoice(MultipartFile soundFile) throws IOException {
        //上传语音到阿里云oss
        String soundUrl = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
        //获取当前用户id
        Long id = UserHolder.get().getId();

        //调用rpc保存
        voiceService.saveVoice(soundUrl, id);

    }

    //接收语音
    public ResponseEntity getVoice() {
        //查询当前使用者id
        Long userId = UserHolder.get().getId();
        //查询剩余次数
        Integer times = userVoiceService.findTimesById(userId);

        //声明返回对象
        VoiceVo voiceVo = new VoiceVo();

        //判断
        if (times > 0) {
            //对剩余次数进行修改
            userVoiceService.updateTimesById(userId, times - 1);
            Integer pageNum = 0;
            voiceVo.setRemainingTimes(times);
            //从语音池获取语音
            while (true) {
                Voice voice = voiceService.findVoiceByCreated(pageNum);
                pageNum++;
                if (voice == null) {//语音池没有语音了
                    voiceVo.setAvatar("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_11.jpg");//头像
                    voiceVo.setNickname("现在语音池没有语音呢,不好意思");
                    voiceVo.setSoundUrl("");//语音地址
                    voiceVo.setGander("女");//性别
                    voiceVo.setRemainingTimes(0);
                  return ResponseEntity.ok(voiceVo);
                }
                if (voice.getUserId() != userId) {//不是自己的语音
                    //从语音池修改这条语音已被拾取
                    voiceService.updateVoice(voice);
                    //封装vo并返回
                    //查询发语音者用户详情
                    UserInfo userInfo = userInfoService.findUserInfoById(voice.getUserId());
                    voiceVo.setId(userInfo.getId().intValue());//发语音者id
                    voiceVo.setAvatar(userInfo.getAvatar());//头像
                    voiceVo.setNickname(userInfo.getNickname());//昵称
                    voiceVo.setAge(userInfo.getAge());//年龄
                    voiceVo.setGander(userInfo.getGender());//性别
                    voiceVo.setSoundUrl(voice.getSoundUrl());//语音地址
                    return ResponseEntity.ok(voiceVo);
                }
            }
        }else {
            voiceVo.setAvatar("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_11.jpg");//头像
            voiceVo.setNickname("您已经没有次数了呢");
            voiceVo.setSoundUrl("");//语音地址
            voiceVo.setGander("女");//性别
            voiceVo.setRemainingTimes(0);
        }

        return ResponseEntity.ok(voiceVo);
    }
}
